package com.hs.srm.admin.modules.orders.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hs.srm.admin.modules.orders.dao.ScmPoHeaderDao;
import com.hs.srm.admin.modules.orders.entity.ScmPoHeaderEntity;
import com.hs.srm.admin.modules.orders.service.ScmPoHeaderService;
import com.hs.srm.admin.modules.orders.vo.ScmPoItemVo;
import com.hs.srm.base.common.utils.QueryParam;
import com.hs.srm.common.utils.PageUtils;

/**
 * 采购订单头信息Service实现
 *
 * @author Sunnykid
 * @date 2019年3月17日
 */
@Service("scmPoHeaderService")
public class ScmPoHeaderServiceImpl extends ServiceImpl<ScmPoHeaderDao, ScmPoHeaderEntity> implements ScmPoHeaderService {

    @Autowired
    private ScmPoHeaderDao scmPoHeaderDao;

    @Override
    public ScmPoHeaderEntity getByPoNo(String poNo) {
        return this.scmPoHeaderDao.getByPoNo(poNo);
    }

    @Override
    public int getEdition() {
        return this.scmPoHeaderDao.getEdition();
    }

    @Override
    public String selectnum() {
        return this.scmPoHeaderDao.selectnum();
    }

    @Override
    public PageUtils findByStock(Map<String, Object> params) {
        Page<ScmPoHeaderEntity> page = this.selectDetailPage(new QueryParam<ScmPoHeaderEntity>(params).getPage(),
                new EntityWrapper<>(), params);
        return new PageUtils(page);
    }

    //根据id更新供应商id
    @Override
    public boolean updateVendorById(ScmPoHeaderEntity headerEntity) {
        int row = scmPoHeaderDao.updateVendorById(headerEntity);
        boolean result = false;
        if (row != 0) {
            result = true;
        }
        return result;
    }

    private Page<ScmPoHeaderEntity> selectDetailPage(Page<ScmPoHeaderEntity> page, Wrapper<ScmPoHeaderEntity> wrapper,
                                                     Map<String, Object> params) {
        page.setRecords(this.baseMapper.findByStock(page, wrapper, params));
        return page;
    }


    @Override
    public PageUtils selectDetailPage(Map<String, Object> params) {
        Page<ScmPoItemVo> page = this.selectDetailPageOne(new QueryParam<ScmPoItemVo>(params).getPage(),
                new EntityWrapper<>(), params);
        return new PageUtils(page);
    }

    private Page<ScmPoItemVo> selectDetailPageOne(Page<ScmPoItemVo> page, Wrapper<ScmPoItemVo> wrapper,
                                                  Map<String, Object> params) {
        page.setRecords(this.baseMapper.selectDetailPage(page, wrapper, params));
        return page;
    }


}
